package com.ymatou.iapi.sellerproduct.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymatou.iapi.productitem.service.GetProductInfoByProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;

/**
*置顶商品Call
*FreeMaker Template自动生成代码
*/
public class BatchDeleteProductsCall extends YmatouAutoTestCall{
	private static String opurl ="/api/SellerProduct/BatchDeleteProducts";

	public BatchDeleteProductsCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json;charset=UTF-8");
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 删除商品公用方法
	 * @param productId
	 * @param sellerId
	 * @throws Exception
	 */
	public void batchDeleteProduct(String productId , int sellerId) throws Exception {
	    try {
	    	
	    	List<String> list=new ArrayList<>();
			list.add(productId);
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = new BatchDeleteProductsCall().callAndGetReturnData(calldate);

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	
	
	
	

	/**
	 * batchDeleteProducts接口返回信息公用错误信息部分断言（正向Case场景）
	 * @param dataStr 传入报文信息
	 * @throws Exception 错误信息
	 */
	public void batchDeleteProductsreturncode(JSONObject dataStr) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("操作成功", msg, "Msg操作成功");
			//String trace =  dataStr.getString("Trace");
			//Logger.verifyIsNull(trace, "Trace等于空");
			//取出报文中CantDeleteProductsNames（无法删除的商品名称）
			JSONArray cantDeleteProductsNames = (JSONArray) dataStr.getJSONObject("Data").getJSONArray("CantDeleteProductsNames");
			if (cantDeleteProductsNames == null || cantDeleteProductsNames.length()<=0) {
				cantDeleteProductsNames=null;
				Logger.verifyIsNull(cantDeleteProductsNames, "删除商品成功，CantDeleteProductsNames应为空");
			}else {
				Logger.verifyIsNull(cantDeleteProductsNames, "删除商品成功，CantDeleteProductsNames应为空");
			}

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	/**
	 * 错误信息断言
	 * @param dataStr 传入报文信息
	 * @param scode 错误代码
	 * @param sBcode 业务代码
	 * @param sMsg 错误信息
	 * @throws Exception
	 */
	public void batchDeleteProductsreturnerrcode(JSONObject dataStr,int scode,int sBcode,String sMsg) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(scode, code, "\"Code\":201");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(sBcode, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains(sMsg, msg, "\"BCode\":0");
			String data =  dataStr.getString("Data");
			Logger.verifyIsNull(data, "Data等于空");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	/**
	 * 正确删除商品后的断言
	 * @param productId1 商品id
	 * @throws Exception
	 */
	public void truever_step(String productId1,int liveId) throws Exception {
	    try {
	    	
			//写integratedproduct新库
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productId1);
			int iActionbyPI = ymtProductsInte.get(0).getiAction();
			Logger.verifyEquals(true, iActionbyPI<0, "新库YmtProducts表IAction置为-1");
			
//			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
//			List<YmtCatalogsI> ymtCatalogsInte = ymtCatalogsIWapper.selectCatalogsByProductId(productId1);
//			int iActionbyCI = ymtCatalogsInte.get(0).getiAction();
//			Logger.verifyEquals(0, iActionbyCI, "新库YmtCatalogs表IAction不变为0");
			
			
			if (liveId!=0) {
				//check SQL YmtProductsInLiveWapper
				YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
				Logger.verifyEquals(1, ymtProductsInLiveWapper.selectByLiveId(liveId).size(),"YmtProductsInLiveWapper 移出直播，记录清除");
				Logger.verifyEquals(-1, ymtProductsInLiveWapper.selectByLiveId(liveId).get(0).getiAction(),"YmtProductsInLiveWapper iaction记录置为-1");
			}
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
		        JSONObject aa=GetProductInfoByProductIdCall.GetProductInfoByProductId(productId1);
				JSONObject resultbymango = aa.getJSONObject("Data").getJSONObject("Product");
				int iActionbymango = resultbymango.getJSONObject("Product").getInt("Action");
				int iStatusbymango = resultbymango.getJSONObject("Product").getInt("Status");
				Logger.verifyEquals(-1, iActionbymango, "Mango中Action置为-1");
				Logger.verifyEquals(-1, iStatusbymango, "Mango中Status置为-1");
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//新Mongo ProductDetailModel
				ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
				Logger.verifyEquals(1, productDetailModelWapper.getCountByProductId(productId1), "商品数据应该只有1条");
				
				Map<String,Object> productDetailModel = productDetailModelWapper.getProductId(productId1);
				Logger.verifyEquals(-1, productDetailModel.get("Action"), "newMango - ProductDetailModel中Action置为-1");
				
				
				//新Mongo ProductListingModel
				ProductListingModelWapper productListingModelWapper =new ProductListingModelWapper();
				Logger.verifyEquals(1, productListingModelWapper.getCountByProductId(productId1), "商品数据应该只有1条");
				
				Map<String,Object> productListingModel = productListingModelWapper.getProductId(productId1);
				Logger.verifyEquals(-1, productListingModel.get("Action"), "newMango - ProductListingModel中Action置为-1");	
				
				if (liveId!=0) {
					//check Mongo LiveProductModelWapper
					LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
					Logger.verifyEquals(0, liveProductModelWapper.getCountByActivityId(liveId),"LiveProductModelWapper 移出直播，记录清除");
				}
				
			}			
						
			
	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	/**
	 * 无法删除商品后的断言
	 * @param productId1 商品id
	 * @param status Mango中Status期待值
	 * @param liveId 直播id，如果没有输入0
	 * @throws Exception
	 */
	public void cantdeletever_step(String productId1,int status,int liveId) throws Exception {
	    try {
	    	
			//写integratedproduct新库
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper(); 
			List<YmtProductsI> ymtProductsInte = ymtProductsIWapper.selectProductByProductId(productId1);
			int iActionbyPI = ymtProductsInte.get(0).getiAction();
			Logger.verifyEquals(0, iActionbyPI, "新库YmtProducts表IAction不变");
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			List<YmtCatalogsI> ymtCatalogsInte = ymtCatalogsIWapper.selectCatalogsByProductId(productId1);
			int iActionbyCI = ymtCatalogsInte.get(0).getiAction();
			Logger.verifyEquals(0, iActionbyCI, "新库YmtProducts表IAction不变");
			
			if (liveId!=0) {
				//check SQL YmtProductsInLiveWapper
				YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
				Logger.verifyEquals(1, ymtProductsInLiveWapper.selectByLiveId(liveId).size(),"YmtProductsInLiveWapper 移出直播，记录清除");
				Logger.verifyEquals(0, ymtProductsInLiveWapper.selectByLiveId(liveId).get(0).getiAction(),"YmtProductsInLiveWapper iaction记录置为-1");
			}
				
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango				
				Map<String,Object> productDetailModel = new ProductBasicInfoWapper().getByProductId(productId1);			
				int iActionbymango = (int) productDetailModel.get("Action");
				int iStatusbymango = (int) productDetailModel.get("Status");
				Logger.verifyEquals(0, iActionbymango, "Mango中Action不变");
				Logger.verifyEquals(status, iStatusbymango, "Mango中Status不变");
				
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//新Mongo
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productId1);
				Logger.verifyEquals(0, productDetailModel.get("Action"), "newMango - ProductDetailModel中Action置为-1");
				
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productId1);
				Logger.verifyEquals(0, productListingModel.get("Action"), "newMango - ProductListingModel中Action置为-1");				
				
				if (liveId!=0) {
					//check Mongo LiveProductModelWapper
					LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
					Logger.verifyEquals(1, liveProductModelWapper.getCountByActivityId(liveId),"无法移出直播，LiveProductModelWapper 记录不清除");
				}
			}			
			
			
			
	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}